//
//  WQLoginViewController.m
//  WisdomQuestor
//
//  Created by 纪景城 on 2024/12/19.
//

#import "WQLoginViewController.h"

@interface WQLoginViewController ()

@property (nonatomic, strong) UIImageView           *backColorImageView;

@property (nonatomic, strong) UIImageView           *logoImageView;
@property (nonatomic, strong) UILabel           *huanYingLabel;
@property (nonatomic, strong) UILabel           *zhongTaiLabel;

@property (nonatomic, strong) UILabel           *accountLabel; // 帐号
@property (nonatomic, strong) BaseTextField           *accountTf;
@property (nonatomic, strong) UILabel           *passwordLabel; // 密码
@property (nonatomic, strong) BaseTextField           *passwordtTf;

@property (nonatomic, strong) UIButton           *registerButton; // 注册
@property (nonatomic, strong) UIButton           *forgetButton; // 忘记密码

@property (nonatomic, strong) UIButton           *loginButton; // 登陆
@property (nonatomic, strong) YYLabel           *urlYYLabel;

@end

@implementation WQLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.edgesForExtendedLayout = UIRectEdgeAll;
    self.hbd_barTintColor = UIColor.clearColor;
    self.hbd_barAlpha = 0;
    [self setWhiteBackBtn];
    
    [self.view addSubviews:[NSArray arrayWithObjects:self.backColorImageView, self.logoImageView, self.huanYingLabel, self.zhongTaiLabel, self.accountLabel, self.accountTf, self.accountTf, self.passwordLabel, self.passwordtTf, self.registerButton, self.forgetButton, self.registerButton, self.forgetButton, self.loginButton, self.urlYYLabel, nil]];
    [self.view insertSubview:self.backColorImageView atIndex:0];
    [self fragmentUI_ViewSet];
    
    
    // 发送手机号验证码 type 1：使用手机号码注册账号时，发送验证码 2：使用手机号码登录账号时，发送验证码 3：重置手机号码注册的账号的密码时，发送验证码
//    [[ThingSmartUser sharedInstance] sendVerifyCodeWithUserName:@"13682303998" region:[[ThingSmartUser sharedInstance] getDefaultRegionWithCountryCode:@"86"] countryCode:@"86" type:1 success:^{
//        DEBUGLOG(@"sendVerifyCode success");
//        [OCProgress_HUD showSuccessWithStatus:@"验证码发送成功"];
//    } failure:^(NSError *error) {
//        DEBUGLOG(@"sendVerifyCode failure: %@", error);
//    }];
    
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//         // 注册手机号帐号
//        [[ThingSmartUser sharedInstance] registerByPhone:@"86" phoneNumber:@"13682303998" password:@"12345678" code:@"882780" success:^{
//            DEBUGLOG(@"register success");
//            [OCProgress_HUD showSuccessWithStatus:@"注册成功"];
//        } failure:^(NSError *error) {
//            DEBUGLOG(@"register failure: %@", error);
//            [OCProgress_HUD showSuccessWithStatus:@"注册失败"];
//        }];
//    });

}

- (void)loginButtonClick {
    if (self.accountTf.text.length == 0) {
        [OCProgress_HUD showErrorWithStatus:@"请输入帐号"];
        return;
    }
    if (self.passwordtTf.text.length == 0) {
        [OCProgress_HUD showErrorWithStatus:@"请输入密码"];
        return;
    }
    
    // 判断是手机号，还是邮箱
    if ([self.accountTf.text containsString:@"@"]) {
        
//        [[ThingSmartUser sharedInstance] loginByEmail:@"86" email:self.accountTf.text password:self.passwordtTf.text success:^{
//            
//            NSLog(@"login success");
//            
//        } failure:^(NSError *error) {
//            NSLog(@"login failure: %@", error);
//        }];
        
        
    } else {
        
        [[ThingSmartUser sharedInstance] loginByPhone:@"86" phoneNumber:self.accountTf.text password:self.passwordtTf.text success:^{
            
            [OCProgress_HUD showSuccessWithStatus:@"登陆成功"];
            
            [WQShare updateHomeDetail];
            
            [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationLoginSuccess object:nil];
            [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationUserInfoUpdate object:nil];
            [self.navigationController popViewControllerAnimated:YES];
            
        } failure:^(NSError *error) {
            DEBUGLOG(@"login failure: %@", error);
            [OCProgress_HUD showSuccessWithStatus:@"登陆失败"];
        }];
    }
} 

#pragma mark ------- Lazy Methods -------

- (void)fragmentUI_ViewSet {
    [self.backColorImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    [self.logoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(kFragmemtFloat(36));
        make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop).offset(kFragmemtFloat(40));
        make.size.mas_equalTo(kSizzleKapadate(36, 36));
    }];
    [self.huanYingLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.logoImageView);
        make.top.equalTo(self.logoImageView.mas_bottom).offset(kFragmemtFloat(20));
    }];
    [self.zhongTaiLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.huanYingLabel);
        make.top.equalTo(self.huanYingLabel.mas_bottom).offset(kFragmemtFloat(10));
    }];
    [self.accountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.huanYingLabel);
        make.top.equalTo(self.zhongTaiLabel.mas_bottom).offset(kFragmemtFloat(46));
    }];
    [self.accountTf mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(kFragmemtFloat(20));
        make.right.equalTo(self.view.mas_right).offset(-kFragmemtFloat(20));
        make.top.equalTo(self.accountLabel.mas_bottom).offset(kFragmemtFloat(12));
        make.height.mas_equalTo(kFragmemtFloat(48));
    }];
    [self.passwordLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.huanYingLabel);
        make.top.equalTo(self.accountTf.mas_bottom).offset(kFragmemtFloat(20));
    }];
    [self.passwordtTf mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(kFragmemtFloat(20));
        make.right.equalTo(self.view.mas_right).offset(-kFragmemtFloat(20));
        make.top.equalTo(self.passwordLabel.mas_bottom).offset(kFragmemtFloat(12));
        make.height.equalTo(self.accountTf);
    }];
    [self.registerButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.passwordtTf.mas_bottom).offset(kFragmemtFloat(14));
        make.left.equalTo(self.passwordtTf.mas_left).offset(kFragmemtFloat(14));
    }];
    [self.forgetButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.registerButton);
        make.right.equalTo(self.passwordtTf.mas_right).offset(-kFragmemtFloat(14));
    }];
    [self.loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(kFragmemtFloat(20));
        make.right.equalTo(self.view.mas_right).offset(-kFragmemtFloat(20));
        make.top.equalTo(self.passwordtTf.mas_bottom).offset(kFragmemtFloat(46));
        make.height.equalTo(self.accountTf);
    }];
    [self.urlYYLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.loginButton.mas_bottom).offset(kFragmemtFloat(15));
    }];
}

- (UIImageView *)backColorImageView {
    if (!_backColorImageView) {
        _backColorImageView = [[UIImageView alloc] initWithImage:kImageName(@"login_bg_image")];
        _backColorImageView.contentMode = UIViewContentModeScaleAspectFill;
    }
    return _backColorImageView;
}

- (UIImageView *)logoImageView {
    if (!_logoImageView) {
        _logoImageView = [[UIImageView alloc] initWithImage:kImageName(@"login_logo_yuanqiu")];
    }
    return _logoImageView;
}

- (UILabel *)huanYingLabel {
    if (!_huanYingLabel) {
        _huanYingLabel = [UILabel initWithTextColor:UIColor.whiteColor weight:UIFontWeightMedium size:15];
        _huanYingLabel.text = @"欢迎来到";
    }
    return _huanYingLabel;
}

- (UILabel *)zhongTaiLabel {
    if (!_zhongTaiLabel) {
        _zhongTaiLabel = [UILabel initWithTextColor:UIColor.whiteColor weight:UIFontWeightMedium size:20];
        _zhongTaiLabel.text = @"中宏低碳科技";
    }
    return _zhongTaiLabel;
}

- (UILabel *)accountLabel {
    if (!_accountLabel) {
        _accountLabel = [UILabel initWithTextColor:UIColor.whiteColor weight:UIFontWeightMedium size:15];
        _accountLabel.text = @"账号";
    }
    return _accountLabel;
}

- (BaseTextField *)accountTf {
    if (!_accountTf) {
        _accountTf = [[BaseTextField alloc] init];
        _accountTf.placeholder = @"请输入帐号";
        
//        _accountTf.clearButtonMode = UITextFieldViewModeWhileEditing;
    }
    return _accountTf;
}

- (UILabel *)passwordLabel {
    if (!_passwordLabel) {
        _passwordLabel = [UILabel initWithTextColor:UIColor.whiteColor weight:UIFontWeightMedium size:15];
        _passwordLabel.text = @"密码";
    }
    return _passwordLabel;
}

- (BaseTextField *)passwordtTf {
    if (!_passwordtTf) {
        _passwordtTf = [[BaseTextField alloc] init];
        _passwordtTf.placeholder = @"请输入密码";
        
//        _passwordtTf.clearButtonMode = UITextFieldViewModeWhileEditing;
    }
    return _passwordtTf;
}

- (UIButton *)registerButton {
    if (!_registerButton) {
        _registerButton = [UIButton initWithTitle:@"注册账号" titleColor:[UIColor.whiteColor colorWithAlphaComponent:0.8] mediumFontSize:12];

        kWEAKSELF
        [_registerButton kj_addAction:^(UIButton * _Nonnull kButton) {
            
        }];
    }
    return _registerButton;
}

- (UIButton *)forgetButton {
    if (!_forgetButton) {
        _forgetButton = [UIButton initWithTitle:@"忘记密码？" titleColor:[UIColor.whiteColor colorWithAlphaComponent:0.8] mediumFontSize:12];

        kWEAKSELF
        [_forgetButton kj_addAction:^(UIButton * _Nonnull kButton) {
            
        }];
    }
    return _forgetButton;
}

- (UIButton *)loginButton {
    if (!_loginButton) {
        _loginButton = [UIButton initWithTitle:@"登 录" titleColor:UIColor.whiteColor boldFontSize:15];
        _loginButton.backgroundColor = [k_Color_3EA2E1 kj_gradientAcrossToColor:k_Color_21D3C2 width:kScreen_Width - kFragmemtFloat(40)];
        _loginButton.cornerRadius = kFragmemtFloat(24);
        [_loginButton addTarget:self action:@selector(loginButtonClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _loginButton;
}

- (YYLabel *)urlYYLabel {
    if (!_urlYYLabel) {
        _urlYYLabel = [[YYLabel alloc] init];
        _urlYYLabel.numberOfLines = 0;
        _urlYYLabel.font = kAdaptedMediumFontSize(12);
        _urlYYLabel.textAlignment = NSTextAlignmentCenter;
        
        NSString *str0 = @"登录即同意";
        NSString *str1 = @"《用户协议》";
        NSString *str2 = @"《隐私协议》";
        
        NSString *str3 = [NSString stringWithFormat:@"%@%@%@", str0, str1, str2];
        NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:str3];
        [str setYy_color:k_Color_C5C3C3];
        
        [str yy_setTextHighlightRange:[str3 rangeOfString:str1] color:[UIColor.whiteColor colorWithAlphaComponent:0.8] backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
            
            WKWebViewController *vc = [[WKWebViewController alloc] initWithUrlSting:@""];
            [[UIApplication currentViewController].navigationController pushViewController:vc animated:YES];
        }];
        
        [str yy_setTextHighlightRange:[str3 rangeOfString:str2] color:[UIColor.whiteColor colorWithAlphaComponent:0.8] backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
            
            WKWebViewController *vc = [[WKWebViewController alloc] initWithUrlSting:@""];
            [[UIApplication currentViewController].navigationController pushViewController:vc animated:YES];
        }];
        
        _urlYYLabel.attributedText = str;
    }
    return _urlYYLabel;
}

@end
